<?php
/**
 * 系统任务
 * Enter description here ...
 * @author Administrator
 *
 */
class Module_overlordtask{
	/**
	 * 构造函数
	 */
	function __construct() {
	
	}
	function index(){
		return $this->opentask();
	}
	/**
	 * 进入任务,更新最新任务
	 */
	function opentask(){
		$time = isset($_SERVER['REQUEST_TIME']) ? $_SERVER['REQUEST_TIME'] : time();
		$uin = SessionInfo('uin');
		$task_key = 'task_' .$uin;
		loadLib('App_Overlord_Task');
		loadLib('App_Overlord_GameStart');
		loadLib('App_Overlord_Query');
		$smarty = loadSmarty();
		//任务列表
		$tasklist = App_Overlord_Task::getTaskList();
	
		
		//查找君主资料
		$user_info = App_Overlord_Query::oneUser($uin);
		if (empty($user_info)){
			return View::error('获取君主信息失败.');
		}
		if (empty($user_info['Value']['task'])){
		//任务类型
			$type = 'freshman';
		}else{	
			$type = 'main';
		}
		
		// ============================================== 查询兵营练兵状态 =======================================
		$barracks_info = App_Overlord_Query::oneState($uin);
		if (!empty($barracks_info['Value']['train_barracks'])){
			$barracks_type = App_Overlord_GameStart::getBarracksType($user_info['Value']['country']);
			foreach ($barracks_info['Value']['train_barracks'] as $k => $row){
				if ($row['time'] <= $time){
					$tiem_list[$k] = $row;	//看有没已练好的兵，先把数量加上
				}
			}
		}
		
		// =================================== 查询兵营 ====================================
		$barracks_count_info = App_Overlord_Query::oneSoldiers($uin);
		$barracks_count = 0;

		//计算士兵总量
		if (!empty($barracks_count_info['Value'])){
			foreach ($barracks_count_info['Value'] as $k => $row){
				$barracks_count += $row['count'];
				if (!empty($tiem_list[$row['type']])){
					$barracks_count += $tiem_list[$row['type']]['count'];  
				}
			}
		}
		
		// ==============================================查询武将资料===========================================
		$generals_info = App_Overlord_Query::oneUin_Generals($uin);
		//初始攻占城池数量
		$generals_city_count = 0;
		if (!empty($generals_info['Value'])){
			foreach ($generals_info['Value'] as $k => $row){
				if (!empty($row['to_uin']) && $row['to_uin'] != $uin){
					$generals_city_count += 1;
				}
				$generals_array[] = $row; 
			}
			//武将等级从高到低排序
			if (!empty($generals_array)){
				$generals_max_lv = App_Overlord_GameStart::getGeneralsArrSort($generals_array);
			}
		}
		// ======================================== 触发的任务 ==========================================
		App_Overlord_Task::trigger('lv' , array('lv' => $user_info['Value']['lv']));		// 触发升级任务
		App_Overlord_Task::trigger('exploit' , array('exploit' => $user_info['Value']['exploits']));		// 触发功勋任务
		//兵力总数完成任务
		if (!empty($barracks_count)){
			App_Overlord_Task::trigger('barracks' , array('count' => $barracks_count));		// 触发扩充兵力任务
		}
		//判断是否攻占有城池
		if (!empty($generals_city_count)){
			App_Overlord_Task::trigger('domain' , array('city_count' => $generals_city_count));		// 触发攻占城池任务
		}
		//武将等级任务
		if (!empty($generals_max_lv)){
			App_Overlord_Task::trigger('general' , array('lv' => $generals_max_lv[0]['lv']));		// 触发升级任务
		}
		//显示最新任务
		$k = App_Overlord_Task::getEvent();
		
		if (!empty($k)){
			foreach ($k as $j => $row){
				$list[$row]['name'] =  $tasklist[$type][$row]['name'];
				$list[$row]['state'] =  App_Overlord_Task::getState($type , $row);
				$list[$row]['type'] = $type;
			}		
			$smarty->assign('list',$list);
		}
		$smarty->assign('type',$type);
		
		return $smarty->fetch('app/Overlord/task.tpl.php');
	}
	/**
	 * 完成任务,领取奖励
	 * Enter description here ...
	 */
	function fulfilltask(){
		$id = isset($_POST['id']) ? trim($_POST['id']) : '';
		$type = isset($_POST['type']) ? trim($_POST['type']) : '';
		$time = isset($_SERVER['REQUEST_TIME']) ? $_SERVER['REQUEST_TIME'] : time(); //时间
		$guide_type = isset($_POST['guide_type']) ? trim($_POST['guide_type']) : '';
		
		$uin = SessionInfo('uin');
		$key = 'user_' . $uin;
		$task_key = 'task_' .$uin;
		loadLib('App_Overlord_Task');
		loadLib('App_Overlord_Query');
		//任务列表
		$tasklist = App_Overlord_Task::getTaskList();
		$task_type = App_Overlord_Task::getTaskType();
		//指导任务
		if ($guide_type == 1){
			//收取税金任务
			App_Overlord_Task::getGo('freshman' , $task_type['freshman']['collect']);
		}elseif ($guide_type == 2){
			//招募将领任务
			App_Overlord_Task::getGo('freshman' , $task_type['freshman']['endlnn']);
		}elseif ($guide_type == 3){
			//更换装备任务
			App_Overlord_Task::getGo('freshman' , $task_type['freshman']['generals_equip']);
		}elseif ($guide_type == 4){
			//学习技能任务
			App_Overlord_Task::getGo('freshman' , $task_type['freshman']['skill']);
		}elseif ($guide_type == 5){
			//训练步兵任务
			App_Overlord_Task::getGo('freshman' , $task_type['freshman']['train']);
		}elseif ($guide_type == 6){
			//收复城池任务
			App_Overlord_Task::getGo('freshman' , $task_type['freshman']['cavalryman']);
		}elseif ($guide_type == 7){
			//收复城池任务
			App_Overlord_Task::getGo('freshman' , $task_type['freshman']['recover']);
		}

		//查询任务列表数据
		loadLib('App_Overlord_GameStart');
		$result = App_Overlord_Query::oneTask($uin);
		if (empty($result)){
			return View::error('数据异常.');
		}
		$state =  App_Overlord_Task::getState($type , $id);

		if (empty($state)){
			if (empty($guide_type)){
				return View::error('任务未完成或没有接次任务.');
			}else{
				App_Overlord_GameStart::getHeader(SITEURL . '/app.php?m=overlordguide&a=guide');
				break;
			}
		}
		if (empty($tasklist[$type][$id])){
			return View::error('该任务不存在.');
		}
		$s = App_Overlord_Task::getState($type , $id);
		if ($s == 2){
			if (empty($guide_type)){
				return View::error('你已领取任务奖励!');
			}else{
				App_Overlord_GameStart::getHeader(SITEURL . '/app.php?m=overlordguide&a=guide');
				break;
			}
		}
		//查询个人数据
		$list = App_Overlord_Query::oneUser($uin);
		if (empty($list)){
			return View::error('获取君主ID失败.');
		}
		//查询个人数据
		$items_info = App_Overlord_Query::oneItems($uin);
		if (empty($items_info)){
			$items_info['Value'] = array();
			App_Overlord_Query::addItems($uin , $items_info['Value']);
		}

		foreach ($tasklist[$type][$id]['award'] as $k => $row){
			if ($k == 'gold'){
				$list['Value']['gold'] += $row;
			}
			if ($k == 'exp'){
				$list['Value']['exp'] += $row;
			}
			if ($k == 'equip'){
				$items_info['Value'][$k][$row[0]][$time] = array('id' => $row[1], 'lv' => 0, 'state' => 0, 'upgrade' => 0, 'repair' => 100);
			}
			if ($k == 'study'){
				//秘籍
				if (empty($items_info['Value']['skills'][$k][$row[0]][$row[1]])){
					$items_info['Value']['skills'][$k][$row[0]][$row[1]]['count'] = $row[2];
				}else{
					$items_info['Value']['skills'][$k][$row[0]][$row[1]]['count'] += $row[2];
				}
			}
			if ($k == 'prop'){
				if (empty($items_info['Value'][$row[0]][$row[1]])){
					$items_info['Value'][$row[0]][$row[1]]['count'] = $row[2];
				}else{
					$items_info['Value'][$row[0]][$row[1]]['count'] += $row[2];
				}
			}
			if ($k == 'material'){
				if (empty($items_info['Value'][$row[0]][$row[1]])){
					$items_info['Value'][$row[0]][$row[1]]['count'] = $row[2];
				}else{
					$items_info['Value'][$row[0]][$row[1]]['count'] += $row[2];
				}
			}
		}
	
		$task_count = count($tasklist[$type]);
	
		App_Overlord_Task::setState($type , $id, 2);
		if (empty($guide_type)){
			App_Overlord_GameStart::getHeader(SITEURL . '/app.php?m=overlordtask&a=awardtask&type='.$type.'&id='.$id);
		}else{
			loadLib('App_Overlord_Collocate');
			
			//加载技能列表
			$skills = App_Overlord_Collocate::getSkill();
			//查询配置信息
			$data = App_Overlord_Collocate::getGoods();
			if ($guide_type == 1 || $guide_type == 7){
				$city_info = App_Overlord_GameStart::getOneOverlord('app_overlord_city', 'city_' .$uin);
			}
			if ($guide_type == 3 || $guide_type == 4 || $guide_type == 7){
				//查询个人武将表
				$generals_info = App_Overlord_Query::oneUin_Generals($uin);
				//随机一个武将
				$generals_rand = array_rand($generals_info['Value']);
			}
			if ($guide_type == 7){
				// =================================== 查询兵营 ====================================
				$barracks_info = App_Overlord_Query::oneSoldiers($uin);
			}
			
			//新手指导传值
			if ($guide_type == 1){
				$city_info['Value']['hkee']['time'] = $time + 6 * 3600;
				$citygold = App_Overlord_GameStart::getCityUser(1);
				$list['Value']['gold'] += $citygold;
				//君主经验
				$list['Value']['exp'] += 2;
			}elseif ($guide_type == 2){
				$generals = App_Overlord_Query::allCollocate('app_overlord_generals');
				//找出名将
				foreach ($generals as $k => $row){
					$row['Value']['id'] = $k;
					//名将
					if ($row['Value']['state'] == 1){
						$generals_state[] = $row;
					}
				}
				$generals_rand = array_rand($generals_state);
				
				$value = $generals_state[$generals_rand]['Value'];
		
				//初始化武将带兵数
				$make_count = 500;
				//初始攻击
				$grow_ap = $value['ap'];
				//初始防御力
				$grow_defense = $value['defense'];
				//初始生命力
				$grow_hp = $value['hp'];
				
				//看下招募过来的武将是否有加兵技能
				if (!empty($value['skills']['strengthen'])){
					foreach ($value['skills']['strengthen'] as $k => $row){
						if ($k == 'staff'){
							$skills_num = $skills['strengthen'][$k][$row[$k]]['num'];
							$make_count += $make_count * $skills_num/100;
							break;
						}
					}
				}
				//武技
				if (!empty($row['skills']['study'])){
					foreach ($row['skills']['study'] as $k => $skills_row){
						if ($k == 'ap'){
							//攻击力加成
							$skills_num = $skills['study'][$k][$skills_row]['num'];
							$grow_ap += $grow_ap * $skills_num/100;
						}elseif ($k == 'defense'){
							//防御力加成
							$skills_num = $skills['study'][$k][$skills_row]['num'];
							$grow_defense += $grow_defense * $skills_num/100;
						}elseif ($k == 'hp'){
							//防御力加成
							$skills_num = $skills['study'][$k][$skills_row]['num'];
							$grow_hp += $grow_hp * $skills_num/100;
						}
					}
				}
				$num = rand(3, 4);
				//减少一个招募卡
				$items_info['Value']['recruit'][2]['count'] -= 1;
				/************************写入武将表**********************/
				$is_value[$time] = array('name' => $generals_state[$generals_rand]['Value']['name'], 'generals' => $generals_state[$generals_rand]['Key'] , 'lv' => 1 , 'exp' => 0 , 'ap' => $grow_ap , 'grow_ap' => $value['grow_ap'] , 'defense' => $grow_defense , 'grow_defense' => $value['grow_defense'] , 'hp' => $grow_hp , 'grow_hp' => $value['grow_hp'] , 'make' => array() , 'make_count' => $make_count , 'physical' => 10 , 'equipment' => array() , 'skills' => $value['skills'] , 'num' => $num , 'state' =>0 , 'to_uin' => 0 , 'time' => $time , 'strategy' => 3 , 'generals_state' => $value['state']);
				//第一次写入武将表
				$code = App_Overlord_Query::addUin_Generals($uin , $is_value);
				//名将加5点经验
				$list['Value']['exp'] += 5;
				//扣钱
				$list['Value']['gold'] -= $generals_state[$generals_rand]['Value']['gold'];
			}elseif ($guide_type == 3){
				//查询装备的属性配置
				$equipnature = App_Overlord_Collocate::getEquipNature();
				//初始攻击力
				$num = 0;
				//初始技能加成
				$skills_num = 0;
				//随机一把武器
				$rand = array_rand($items_info['Value']['equip']['weapon']);
				//装备信息
				$items_equip_info = $items_info['Value']['equip']['weapon'][$rand];
				$equip_lv = $data['weapon'][$items_equip_info['id']]['lv'];
				//获取一级不同装备的数值
				//武器
				if ($data['weapon'][$items_equip_info['id']]['num'] == 6){
					$equip_lv = $equip_lv . '_1';
				}elseif ($data['weapon'][$items_equip_info['id']]['num'] == 12){
					$equip_lv = $equip_lv . '_2';
				}
				//装备属性
				$num = $equipnature['weapon'][$equip_lv][$items_equip_info['lv']];
				//武将有这个武器加攻击的技能
				if (!empty($generals_info['Value'][$generals_rand]['skills']['study'])){
					//如果有加攻击技能
					foreach ($generals_info['Value'][$generals_rand]['skills']['study'] as $k => $row){
						if ($k == $type){
							$skills_num = $skills['study'][$k][$row]['num'];
							$num += $num * $skills_num/100;
							break;
						}
					}
				} 
				//将装备的属性加上
				$generals_info['Value'][$generals_rand]['ap'] += $num;
				//将装备改成已带状态
				$items_info['Value']['equip']['weapon'][$rand]['state'] = 1;
				//给武将带上装备
				$generals_info['Value'][$generals_rand]['equipment']['weapon'] = array('id' => $rand, 'name' => $data['weapon'][$items_info['Value']['equip']['weapon'][$rand]['id']]['name'], 'lv' => $items_info['Value']['equip']['weapon'][$rand]['lv'], 'num' => $num);
				
			}elseif ($guide_type == 4){
				$items_info['Value']['skills']['study']['general_ap'][0]['count'] -= 1;
				if (!empty($generals_info['Value'][$generals_rand]['skills'])){
					//存一个数组秘籍
					$generals_info['Value'][$generals_rand]['skills']['study']['general_ap'] = 0;
				}else{
					//存一个数组秘籍
					$array['study']['general_ap'] = 0;
					$generals_info['Value'][$generals_rand]['skills'] = $array;
				}
				//技能孔
				$generals_info['Value'][$generals_rand]['num'] -= 1; 
			}elseif ($guide_type == 5){
				$end_time = 200 * 13 + $time;
				$value = array('count' => 200, 'time' => $end_time);
				//练兵类型，步兵
				$type = 'infantry';
				$list['Value']['exp'] += 3;
			}elseif ($guide_type == 6){
				$end_time = 100 * 13 + $time;
				$value = array('count' => 100, 'time' => $end_time);
				//练兵类型，骑兵
				$type = 'cavalryman';
				$list['Value']['exp'] += 2;
			}elseif ($guide_type == 7){
				//城池
				$city_info['Value']['hkee']['to_uin'] = $uin;
				$city_info['Value']['hkee']['generals'] = $generals_rand;
				$city_info['Value']['hkee']['generals_name'] = $generals_info['Value'][$generals_rand]['name'];
				$city_info['Value']['hkee']['time'] = $time;
				//兵营减100步兵
				$barracks_info['Value'][0]['count'] -= 100;
				//武将修改变化
				$generals_info['Value'][$generals_rand]['make'] = array(100 , 0 , 0 , 0);
				$generals_info['Value'][$generals_rand]['state'] = 1;
				$generals_info['Value'][$generals_rand]['to_uin'] = $uin;
				$generals_info['Value'][$generals_rand]['time'] = $time;
				$generals_info['Value'][$generals_rand]['city_time'] = $time - 5 * 60;       //修改武将驻防城池需调兵时间
				//加钱
				$list['Value']['gold'] += 4000;
				//加经验
				$list['Value']['exp'] += 80;
				//君主完成任务状态
				$list['Value']['task'] = 1;
			}
			// ========================= 指导任务操作修改  ========================
			if ($guide_type == 1 || $guide_type == 7){
				//修改城池状态
				$code = App_Overlord_Query::alterCity($uin, $city_info['Value']);
			}
			if ($guide_type == 3 || $guide_type == 4 || $guide_type == 7){
				//修改武将
				$code = App_Overlord_Query::alterUin_Generals($uin , $generals_info['Value']);
			}
			if ($guide_type == 5 || $guide_type == 6){
				//扣钱
				$list['Value']['gold'] -= 500;
				//练兵
				$state_info = App_Overlord_Query::oneState($uin);
				if (empty($state_info)){
					$is_value = array('train_barracks'=> array($type => $value));
					$code = App_Overlord_Query::addState($uin, $is_value);
				}else{
					$state_info['Value']['train_barracks'][$type] = $value;
					$code = App_Overlord_Query::alterState($uin, $state_info['Value']);
				}
			}
			if ($guide_type == 7){
				//修改兵营
				$code = App_Overlord_Query::alterSoldiers($uin, $barracks_info['Value']);
			}
			App_Overlord_GameStart::getHeader(SITEURL . '/app.php?m=overlordguide&a=guide');

		}
		//写入背包
		if (!empty($items_info)){
			$code = App_Overlord_Query::alterItems($uin, $items_info['Value']);
		}
		
		//修改君主信息
		$code = App_Overlord_Query::alterUser($uin, $list['Value']);
		//完成新手指导
		if ($guide_type == 7){
			//更新任务
			App_Overlord_Task::getEvent();
		}
		
	}
	/**
	 * 任务描述
	 */
	function describetask(){
		$id = isset($_GET['id']) ? trim($_GET['id']) : '';
		$type = isset($_GET['type']) ? trim($_GET['type']) : '';
		loadLib('App_Overlord_Task');
		//任务列表
		$tasklist = App_Overlord_Task::getTaskList();
		
		if (empty($tasklist[$type][$id])) {
			return View::error('没有此类任务描述.');
		}
		loadLib('App_Overlord_Collocate');
		$data = App_Overlord_Collocate::getGoods();
		if (!empty($tasklist[$type][$id]['award']['equip'])){
			$row = $tasklist[$type][$id]['award']['equip'];
			$tasklist[$type][$id]['award']['equip']['name'] = $data[$row[0]][$row[1]]['name'];
			$tasklist[$type][$id]['award']['equip']['count'] = $row[2];
		}
		if (!empty($tasklist[$type][$id]['award']['prop'])){
			$row = $tasklist[$type][$id]['award']['prop'];
			$tasklist[$type][$id]['award']['prop']['name'] = $data[$row[0]][$row[1]]['name'];
			$tasklist[$type][$id]['award']['prop']['count'] = $row[2];
		}
		$smarty = loadSmarty();
		$smarty->assign('list',$tasklist[$type][$id]);
		return $smarty->fetch('app/Overlord/describetask.tpl.php');
	}
	/**
	 * 任务完成奖励
	 */
	function awardtask(){
		$id = isset($_GET['id']) ? trim($_GET['id']) : '';
		$type = isset($_GET['type']) ? trim($_GET['type']) : '';
		loadLib('App_Overlord_Task');
		//任务列表
		$tasklist = App_Overlord_Task::getTaskList();
		if (empty($tasklist[$type][$id])) {
			return View::error('没有此类任务描述.');
		}
		loadLib('App_Overlord_Collocate');
		$data = App_Overlord_Collocate::getGoods();
		if (!empty($tasklist[$type][$id]['award']['equip'])){
			$row = $tasklist[$type][$id]['award']['equip'];
			$tasklist[$type][$id]['award']['equip']['name'] = $data[$row[0]][$row[1]]['name'];
			$tasklist[$type][$id]['award']['equip']['count'] = $row[2];
		}
		if (!empty($tasklist[$type][$id]['award']['prop'])){
			$row = $tasklist[$type][$id]['award']['prop'];
			$tasklist[$type][$id]['award']['prop']['name'] = $data[$row[0]][$row[1]]['name'];
			$tasklist[$type][$id]['award']['prop']['count'] = $row[2];
		}
		if (!empty($tasklist[$type][$id]['award']['material'])){
			$row = $tasklist[$type][$id]['award']['material'];
			$tasklist[$type][$id]['award']['material']['name'] = $data[$row[0]][$row[1]]['name'];
			$tasklist[$type][$id]['award']['material']['count'] = $row[2];
		}
		if (!empty($tasklist[$type][$id]['award']['study'])){
			$skill = App_Overlord_Collocate::getSkill();
			$row = $tasklist[$type][$id]['award']['study'];
			$tasklist[$type][$id]['award']['study']['name'] = $skill['study'][$row[0]][$row[1]]['name'];
			$tasklist[$type][$id]['award']['study']['count'] = $row[2];
		}

		$smarty = loadSmarty();
		$smarty->assign('list',$tasklist[$type][$id]);
		return $smarty->fetch('app/Overlord/awardtask.tpl.php');
	}
}
# end of script